<?php
namespace App\Services;
use App\Models\BlastUserModel;

class UserService
{
	/**
	 * 插入用户数据
	 * @param array $data
	 * @return bool $res 
	 */
	public function insertUser($data){
		$res = BlastUserModel::insert($data);
		return $res;
	}

	/**
	 * 获取用户列表
	 * @param string $check
	 * @return array $userlist
	 */
	public function getUserList($pagesize,$keywords=''){
		$userlist = BlastUserModel::orderBy('id','asc')
					->where('check','normal')
					->where(
						function($query) use ($keywords){
							if($keywords != ''){
								$query->where('username', 'like', '%'.$keywords.'%');
							}
						}
					)
					->select('id','username','name','add_date')
					->paginate($pagesize);

		return $userlist;
	}

	/**
	 * 获取用户信息
	 * @param string $username
	 * @return $userinfo
	 */
	public function getUserInfoByName($username){
		$userinfo = BlastUserModel::where('username',$username)
					->select('id','username')
					->first();
		return $userinfo;
	}

	/**
	 * 获取用户信息
	 * @param int $id
	 * @return array $info
	 */
	public function getUserInfoById($id){
		$userinfo = BlastUserModel::where('id',$id)
					->select('id','username','password','name','token')
					->first()
					->toArray();
		return $userinfo;
	}

	/**
	 * 更新用户信息
	 * @param int $id
	 * @param array $data
	 * @return bool $res
	 */
	public function modifyUser($id,$data){
		$res = BlastUserModel::where('id',$id)->update($data);
		return $res;
	}

}